﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Baobao : Characters {

    static public float speed = 100f;

    public AudioClip[] SwordSounds;
    protected AudioSource aud;

    private Vector2 targetpos = new Vector2(0,0);
    private Rigidbody2D playerrigidbody;
    public Animator anim;
    public AnimatorStateInfo animSta;
    public GameObject[] Attack1Shapes;
    private const string FightIdleState = "BaobaoFightIdle";
    private const string FightRunState = "BaobaoFightRun";
    private const string Attack1State = "BaobaoFight1";
    private const string Attack2State= "BaobaoFight2";
    private const string Attack3State = "BaobaoFight3";
    private int HitCount = 0;
    //private Transform trans;
    public bool HeadRight()
    {
        if (transform.localScale.x == -1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public bool IsMoving(float h, float v)
    {
        if (h != 0 || v !=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /*
        void UpdateAttack()
        {
            if (Input.GetKeyDown(KeyCode.X) && IsMoving(h,v))
            {
                //anim.SetTrigger("Right");
                anim.SetTrigger("Attack1");
            }
        }
        */



    void Attack()
    {
        if (animSta.IsName(FightIdleState) && HitCount == 0 && animSta.normalizedTime > 0.10f)
        {
            anim.SetInteger("Attack", 1);
            HitCount = 1;
        }
        else if (animSta.IsName(Attack1State) && HitCount == 1 && animSta.normalizedTime > 0.1f)
        {
            anim.SetInteger("Attack", 2);
            HitCount = 2;
        }
        else if (animSta.IsName(Attack2State) && HitCount == 2 && animSta.normalizedTime > 0.1f)
        {
            anim.SetInteger("Attack", 3);
            HitCount = 3;
        }
        else
        {
            if (animSta.IsName(FightRunState) && HitCount == 0 && animSta.normalizedTime > 0.10f)
            {
                anim.SetInteger("Attack", 1);
                HitCount = 1;
            }
            else if (animSta.IsName(Attack1State) && HitCount == 1 && animSta.normalizedTime > 0.1f)
            {
                anim.SetInteger("Attack", 2);
                HitCount = 2;
            }
            else if (animSta.IsName(Attack2State) && HitCount == 2 && animSta.normalizedTime > 0.1f)
            {
                anim.SetInteger("Attack", 3);
                HitCount = 3;
            }
        }
    }


    // Use this for initialization
    protected override void Start () {
        playerrigidbody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        //trans = GetComponent<Transform>();
        //anim.SetBool("Right", true);
        anim.SetBool("Left", false);
        anim.SetBool("Walking", false);
        HitCount = 0;

        aud = GetComponent<AudioSource>();
    }
	
	// Update is called once per frame
	void Update () {

        Debug.Log(speed);
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        targetpos = new Vector2(h*speed*0.01f, v*speed*0.01f);
        targetpos += new Vector2(transform.position.x, transform.position.y);
        playerrigidbody.MovePosition(Vector2.MoveTowards(transform.position, targetpos, speed * Time.deltaTime));
        //transform.position = targetpos;

        //攻击
        animSta = anim.GetCurrentAnimatorStateInfo(0);
        if (!animSta.IsName(FightIdleState) && animSta.normalizedTime > 1.0f)
        {
            anim.SetInteger("Attack", 0);
            HitCount = 0;
        }
        if (Input.GetKeyDown(KeyCode.X))
        {
            //anim.SetTrigger("Right");
            //anim.SetTrigger("Attack1");
            Attack();
        }


        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            //anim.SetTrigger("Left");
            anim.SetBool("Left", true);
            anim.SetBool("Right", false);


        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            //anim.SetTrigger("Right");
            anim.SetBool("Left", false);
            anim.SetBool("Right", true);
        }
        if (h != 0)
        {
            transform.localScale = new Vector3(h, 1);
            anim.SetBool("Walking", true);
        }
        else if (v != 0)
        {
            anim.SetBool("Walking", true);
        }
        else
        {
            anim.SetBool("Walking", false);
        }

    }

    public void GenerateAttack1Shape(int attackIndex)
    {
        GameObject AttackShape = GameObject.Instantiate(Attack1Shapes[attackIndex], this.transform.position, Quaternion.identity);
        AttackShape.transform.localScale = this.transform.localScale;
    }

    public void PlaySwordSound(int index)
    {
        //int randomIndex = Random.Range(0, 2);
        aud.PlayOneShot(SwordSounds[index]);
    }
}
